package com.easygo.controller;

import com.easygo.service.ItemCatService;
import com.easygo.utils.PageUtils;
import com.xuexi.easygo.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Scope("prototype")
public class ItemCatController {

    @Autowired
    ItemCatService itemCatService;

    //商品分类的接口
    @RequestMapping("itemcat_page")
    public PageUtils<ItemCat> itemcat_page(@RequestParam(value = "pageIndex",defaultValue = "1",required = false)Integer pageIndex,
                                           @RequestParam(value = "pageSize",defaultValue = "5",required = false)Integer pageSize,
                                           @RequestParam(value = "parent_id",defaultValue = "0",required = false)Integer parent_id){
        Map<String,Object> map = new HashMap<>();
        map.put("pageStart",(pageIndex-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("parent_id",parent_id);

        Integer totalCount = itemCatService.getTotalCount(map);
        List<ItemCat> itemCats = itemCatService.getItemCatsByPage(map);
        return new PageUtils<>(pageSize,pageIndex,totalCount,itemCats);
    }

    @RequestMapping("/itemcat_getById")
    public ItemCat getItemCatById(@RequestParam(value = "id") Integer id){
        return itemCatService.getItemCatById(id);
    }

    /*
    * 所有类目
    * */
    @RequestMapping("/itemcat_all")
    public List<ItemCat> getItemCats(){
        return itemCatService.getItemCats();
    }

    @RequestMapping("addItemCat")
    public Integer addItemCat(@RequestBody ItemCat itemCat){
        return itemCatService.addItemCat(itemCat);
    }
}
